package com.lvyq.myplayer.ui.activity.setting

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.ProgressBar
import com.lvyq.myplayer.R
import com.lvyq.myplayer.base.BaseActivity
import org.jetbrains.anko.find

/**
 * 文件描述：WebViewActivity 
 * 作者：lvyq
 * 创建时间：2025-05-20
 * 修改时间：2025-05-20
 */
class WebViewActivity() :BaseActivity() {

    private lateinit var progressBar: ProgressBar
    private lateinit var webContent:WebView

    override fun getLayoutId(): Int {
       return R.layout.activity_web_view
    }



    override fun initData() {
        var  content= intent.getStringExtra("content").toString()
        webContent = findViewById<WebView>(R.id.web_content)
        progressBar = find(R.id.progress_bar)
        webContent.settings.apply {
            domStorageEnabled = true
            javaScriptEnabled = true
            defaultTextEncodingName = "utf-8"
            loadsImagesAutomatically = true
            useWideViewPort = true
            loadWithOverviewMode = true
            allowFileAccess = true
        }

        webContent.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView?, url: String?) {
                progressBar.visibility = View.GONE
            }
            override fun onReceivedError(
                view: WebView?,
                request: WebResourceRequest?,
                error: WebResourceError?
            ) {
                Log.e("WebViewError", "加载失败: ${error?.description}")
                // 可添加错误处理逻辑（如显示默认文本）
                super.onReceivedError(view, request, error)
            }
        }

        webContent.webChromeClient = object : WebChromeClient() {
            override fun onProgressChanged(view: WebView?, newProgress: Int) {
                progressBar.progress = newProgress
                if (newProgress == 100) progressBar.visibility = View.GONE
            }
        }

        try {
            webContent.loadUrl("file:///android_res/raw/"+content+".html")
        } catch (e: Exception) {
            Log.e("WebViewLoader", "文件加载异常", e)
        }
    }

    companion object {
        fun start(context: Context,type:String) {
            val intent = Intent(context, WebViewActivity::class.java)
            intent.putExtra("content",type)
            context.startActivity(intent)
        }
        }

}